package test;

import java.util.concurrent.ConcurrentLinkedQueue;

public class TaskEngine {
    //
    private volatile ConcurrentLinkedQueue<Command> queue = new ConcurrentLinkedQueue<Command>();
    private TaskMapping taskMapping = new TaskMapping();
    //stop一旦出现true的情况则调用任何命令皆不可以阻止
    //wait可以wait取消命令进行恢复
    //只要stop那么可以直接强制性的抢占掉wait
    private volatile boolean wait = false;
    private volatile boolean stop = false;

    public TaskMapping getTaskMapping() {
        return taskMapping;
    }

    public void setTaskMapping(TaskMapping taskMapping) {
        this.taskMapping = taskMapping;
    }

    public boolean isWait() {
        return wait;
    }

    public void setWait(boolean wait) {
        if (this.isStop()) {
            return;
        }
        this.wait = wait;
    }

    public boolean isStop() {
        return stop;
    }

    public void setStop(boolean stop) {
        if (!stop) {
            throw new IllegalStateException("not false");
        }
        this.stop = stop;
    }

    public void init() {
        System.out.println("init");

    }

    public void service() {
        System.out.println("service");
        try {
            service_ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //

    public void destroy() {
        System.out.println("destroy");
    }

    public ConcurrentLinkedQueue getQueue() {
        return queue;
    }

    public void setQueue(ConcurrentLinkedQueue queue) {
        this.queue = queue;
    }

    public void service_ok() {
        while (!isStop()) {
            //
            Command command = (Command) getQueue().poll();
            if (command != null) {
                //
                if (Command.task_engine_wait.equals(command.getCommandType())) {
                    System.out.println("task_engine_wait");
                    setWait(true);
                }
                if (Command.task_engine_wait_cancel.equals(command.getCommandType())) {
                    System.out.println("task_engine_wait_cancel");
                    setWait(false);
                }
                if (Command.task_engine_stop.equals(command.getCommandType())) {
                    System.out.println("task_engine_stop");
                    setStop(true);
                }

                //
                if (Command.task_create.equals(command.getCommandType())) {
                    System.out.println("task_create");
                    Task task = new Task(this, command.getId());
                    taskMapping.getTaskMap().put(command.getId(), task);
                    task.start();
                }
                //
                if (Command.task_wait.equals(command.getCommandType())) {
                    System.out.println("task_wait");
                    Task task = taskMapping.getTaskMap().get(command.getId());
                    if (task != null) {
                        task.setWait(true);
                    }

                }
                if (Command.task_wait_cancel.equals(command.getCommandType())) {
                    System.out.println("task_wait_cancel");
                    Task task = taskMapping.getTaskMap().get(command.getId());
                    if (task != null) {
                        task.setWait(false);
                    }

                }
                if (Command.task_stop.equals(command.getCommandType())) {
                    System.out.println("task_stop");
                    Task task = taskMapping.getTaskMap().get(command.getId());
                    if (task != null) {
                        task.setStop(true);
                    }

                }

            }
        }
        System.out.println("end");
    }
}
